/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import DAO.DAOFactory;
import DAO.DAODomicilio;
import javax.swing.JOptionPane;
import modelo.Domicilio;
import modelo.LimitadorCaracteres;

/**
 *
 * @author JGS
 */
public class Alta_Direccion extends javax.swing.JInternalFrame {

    private SingletonDomicilios domicilios;
    private int ind = -1;

    private static Alta_Direccion instancia = null;

    public static Alta_Direccion getInstancia(int id) {
        if (instancia == null) {
            instancia = new Alta_Direccion(id);
        } else {
            instancia.dispose();
            instancia = new Alta_Direccion(id);
        }
        return instancia;
    }

    public static Alta_Direccion getInstancia() {
        if (instancia == null) {
            instancia = new Alta_Direccion();
        } else {
            instancia.dispose();
            instancia = new Alta_Direccion();
        }
        return instancia;
    }

    /**
     * Creates new form Alta_Direccion
     */

    private Alta_Direccion(int ind) {
        super("Modificacion Direccion",
                false, //resizable
                false, //closable
                false, //maximizable
                false);//iconifiable
        initComponents();
        domicilios = SingletonDomicilios.getInstancia();
        Domicilio aux = domicilios.getDomicilios().get(ind);
        this.calle.setText(aux.getCalle());
        this.numero.setText(aux.getNumero() + "");
        this.provincia.setText(aux.getProvincia());
        this.localidad.setText(aux.getLocalidad());
        this.barrio.setText(aux.getBarrio());
        this.piso.setText(aux.getPiso() + "");
        this.depto.setText(aux.getDepto());
        this.referencia.setText(aux.getReferencia());
        this.ind = ind;
    }

    public Alta_Direccion() {
        super("Alta Direccion",
                false, //resizable
                false, //closable
                false, //maximizable
                false);//iconifiable

        initComponents();
        domicilios = SingletonDomicilios.getInstancia();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel9 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        numero = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        piso = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        referencia = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        calle = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        depto = new javax.swing.JTextField();
        barrio = new javax.swing.JTextField();
        localidad = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        provincia = new javax.swing.JTextField();

        jLabel9.setText("jLabel9");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(153, 153, 153));
        jLabel6.setText("Alta Dirección");

        jLabel1.setText("Calle:");

        jLabel2.setText("N°:");

        jLabel3.setText("Piso:");

        jLabel4.setText("Referencia:");

        jLabel5.setText("Barrio:");

        jLabel7.setText("Localidad:");

        jButton4.setText("Grabar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Cerrar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel8.setText("Depto:");

        jLabel10.setText("Provincia:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel10))
                                .addGap(6, 6, 6)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(calle, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                                    .addComponent(localidad, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addGap(18, 18, 18)
                                        .addComponent(piso, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel8)
                                        .addGap(18, 18, 18)
                                        .addComponent(depto, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(referencia, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                                    .addComponent(barrio, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                                    .addComponent(provincia, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)))
                            .addComponent(jButton4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton5)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(numero, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton4, jButton5});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(calle, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(numero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(piso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(depto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(referencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(localidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(barrio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel5)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(provincia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        numero.setDocument (new LimitadorCaracteres(4,true));
        piso.setDocument (new LimitadorCaracteres(2,true));
        referencia.setDocument (new LimitadorCaracteres(25,false));
        calle.setDocument (new LimitadorCaracteres(100,false));
        depto.setDocument (new LimitadorCaracteres(2,false));
        barrio.setDocument (new LimitadorCaracteres(25,false));
        localidad.setDocument (new LimitadorCaracteres(50,false));
        provincia.setDocument (new LimitadorCaracteres(50,false));

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    if (this.checkFields()) {
        DAODomicilio dao = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getDomicilioDAO();
        Domicilio toReg = new Domicilio();
        toReg.setBarrio(barrio.getText());
        toReg.setCalle(calle.getText());
        toReg.setDepto(depto.getText());
        toReg.setLocalidad(localidad.getText());
        toReg.setNumero(Integer.parseInt(numero.getText()));
        try {
            toReg.setPiso(Integer.parseInt(piso.getText()));
        } catch (Exception ex) {
            toReg.setPiso(0);
        }
        toReg.setProvincia(provincia.getText());
        toReg.setReferencia(referencia.getText());
        //toReg.setUsuarioID(1); //de donde mierda sacamos el usuario?? 0.o
        toReg.setUsuarioID(10000);
        if (ind == -1) this.domicilios.agregarDomicilio(toReg);
        else {
            this.domicilios.getDomicilios().remove(ind);
            this.domicilios.agregarDomicilio(toReg);
        }

        this.domicilios.getPantalla().refrescarPantalla();
        this.dispose();
    }


}//GEN-LAST:event_jButton4ActionPerformed

    private boolean checkFields() {
        if (calle.getText().length() == 0) {
            JOptionPane.showMessageDialog(this, "Ingrese una calle");
            return false;
        }
        if (localidad.getText().length() == 0) {
            JOptionPane.showMessageDialog(this, "Ingrese una localidad");
            return false;
        }
        if (provincia.getText().length() == 0) {
            JOptionPane.showMessageDialog(this, "Ingrese una provincia");
            return false;
        }
        if (numero.getText().length() == 0) {
            JOptionPane.showMessageDialog(this, "Ingrese un numero");
            return false;
        }

        try {
            Integer.parseInt(numero.getText());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Ingrese un numero valido");
            return false;
        }
        return true;
    }

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
// TODO add your handling code here:
    this.domicilios.getPantalla().refrescarPantalla();
    this.dispose();
}//GEN-LAST:event_jButton5ActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField barrio;
    private javax.swing.JTextField calle;
    private javax.swing.JTextField depto;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField localidad;
    private javax.swing.JTextField numero;
    private javax.swing.JTextField piso;
    private javax.swing.JTextField provincia;
    private javax.swing.JTextField referencia;
    // End of variables declaration//GEN-END:variables
}


